<?php

App::uses('AppModel', 'Model');

/**
 * Category Model
 *
 * @property Category $ParentCategory
 * @property Category $ChildCategory
 */
class Category extends AppModel {

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'name';

    /**
     * @var array
     */
    public $actsAs = array(
        'Tree' => array(
            'left' => 'lft',
            'right' => 'rght'
        )
    );

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'parent_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Your custom message here',
                'allowEmpty' => true,
            ),
        ),
        'lft' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Your custom message here',
                'allowEmpty' => true,
            ),
        ),
        'rght' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Your custom message here',
                'allowEmpty' => true,
            ),
        ),
        'name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Category name not empty.',
            ),
        ),
        'del_flg' => array(
            'boolean' => array(
                'rule' => array('boolean'),
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'ParentCategory' => array(
            'className' => 'Category',
            'foreignKey' => 'parent_id',
            'conditions' => array('ParentCategory.del_flg' => 0),
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'ChildCategory' => array(
            'className' => 'Category',
            'foreignKey' => 'parent_id',
            'dependent' => false,
            'conditions' => array('ChildCategory.del_flg' => 0),
        )
    );

}
